package com.wwwarehouse.resource_center.adapter.salesInventory_statistics;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.custom_widget.progressbar.CircleProgressView;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.sales_inventory_statistics.SalesInventoryStatisticsResponseBean;
import com.wwwarehouse.resource_center.fragment.sales_Inventory_statistics.SalesInventoryStatisticsListFragment;

import java.util.ArrayList;

/**
 * Created by xiaoke.ni on 2017/10/31.
 */

public class SalesInventoryStatisticsAdapter extends RecyclerView.Adapter<SalesInventoryStatisticsAdapter.Holder> {

    private ArrayList<SalesInventoryStatisticsResponseBean.ListBean> dataList;
    private Context mContext;
    private int mHeight;

    public SalesInventoryStatisticsAdapter(Context mContext,ArrayList<SalesInventoryStatisticsResponseBean.ListBean> dataList,int mHeight){
        this.mContext = mContext;
        this.dataList = dataList;
        this.mHeight = mHeight;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sales_inventory_statistics, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        holder.itemView.setLayoutParams(params);
        params.height = (mHeight - ConvertUtils.dip2px(mContext, 20)) / SalesInventoryStatisticsListFragment.SIZE;
        ImageloaderUtils.displayImg(dataList.get(position).getItemUrl(),holder.IconIv);
        holder.mCountTv.setText(String.format(mContext.getString(R.string.sales_inventory_available),dataList.get(position).getValidQty()));
        holder.mCodeTv.setText(String.format(mContext.getString(R.string.sales_inventory_code),dataList.get(position).getItemCode()));
        holder.mSoldTv.setText(String.format(mContext.getString(R.string.sales_inventory_sold),dataList.get(position).getSoldQty()));
        holder.mProgressCpv.setMax(100);
        int validRatio = (int) (dataList.get(position).getValidRatio()*100);
        holder.mProgressCpv.setProgress(validRatio);
        holder.mProgressTv.setText(validRatio + "%");
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    static class Holder extends RecyclerView.ViewHolder {

        public ImageView IconIv;
        public TextView mCountTv;
        public TextView mCodeTv;
        public TextView mSoldTv;
        public CircleProgressView mProgressCpv;
        public TextView mProgressTv;

        public Holder(View view) {
            super(view);
            IconIv = (ImageView) view.findViewById(R.id.iv_icon);
            mCountTv = (TextView) view.findViewById(R.id.count_tv);
            mCodeTv = (TextView) view.findViewById(R.id.code_tv);
            mSoldTv = (TextView) view.findViewById(R.id.sold_tv);
            mProgressCpv = (CircleProgressView) view.findViewById(R.id.progress_cpv);
            mProgressTv = (TextView) view.findViewById(R.id.progress_tv);
        }
    }
}
